package com.xishu.service;

import com.xishu.config.Constant;
import com.xishu.entity.shop.RunModel;
import com.xishu.response.ResponseStatus;
import com.xishu.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class RunModelService implements Tools {
    private static RunModelService instance = new RunModelService();
    private CommonService commonService = CommonService.getInstance();
    private static Logger logger = LoggerFactory.getLogger(RunModelService.class);

    private RunModelService() {
    }

    public static RunModelService getInstance() {
        return instance;
    }

    /**
     * 查询运营模式
     *
     * @param shopId
     * @param type
     * @return
     */
    public RunModel findRunModel(Long shopId, Integer type) {
        RunModel runModel = new RunModel();
        runModel.setShopId(shopId);
        runModel.setModelType(type);
        return commonService.searchOne(runModel);
    }

    /**
     * 查询分店的所有运营模式
     *
     * @param shopId
     * @return
     */
    public List<RunModel> findRunModelList(Long shopId) {
        RunModel runModel = new RunModel();
        runModel.setShopId(shopId);
        runModel.setEntity(true);
        return commonService.searchAll(runModel);
    }

    /**
     * 检查当前的运营模式是否合法
     *
     * @param runModel
     */
    public ResponseStatus checkRunModel(RunModel runModel) throws Exception {
        //总二维码点单里面的堂食开启时，餐桌二维点单，餐桌点单、服务员出单二维码点单不能启用（这三种都是餐桌点餐类型的）
        //自助快餐开启时，餐桌二维点单，餐桌点单、服务员出单二维码点单不能启用（这三种都是餐桌点餐类型的）

        //关闭的时候不检查
        if (!getBoolean(runModel.getEnable())) {
            logger.info("close the enable ,no need to check");
            return ResponseStatus.OK;
        }

        //打开的时候需要检查
        //总二维堂食，或者是自助快餐的时候，不能开启餐桌点餐，餐桌二维码点餐，服务员出单二维码点单
        if (runModel.getModelType() == Constant.RUN_MODEL_TYPE_TOTAL && getBoolean(runModel.getTangshi())) {
            //开启了餐桌模式，返回
            if (foodTableStyleRunModelOpen(runModel.getShopId())) {
                logger.info("total type open failed");
                return ResponseStatus.FOOD_TABLE_MODEL_ENABLE;
            }
        }

        //快餐模式
        if (runModel.getModelType() == Constant.RUN_MODEL_TYPE_QUICK_MODEL) {
            //开启了餐桌模式，返回
            if (foodTableStyleRunModelOpen(runModel.getShopId())) {
                logger.info("quick model open failed");
                return ResponseStatus.FOOD_TABLE_MODEL_ENABLE;
            }
        }

        //餐桌点餐，餐桌二维码点餐，服务员出单二维码点单的时候，不能开启总二维堂食，或者是自助快餐
        if (runModel.getModelType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE || runModel.getModelType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE_FIX || runModel.getModelType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE_WAITER_QR) {
            //开启了快餐模式
            if (quickFoodStyleRunModelOpen(runModel.getShopId())) {
                logger.info("food table open failed");
                return ResponseStatus.QUICK_FOOD_MODEL_ENABLE;
            }
        }

        return ResponseStatus.OK;
    }

    /**
     * 获取餐桌模式的运营模式
     *
     * @param shopId
     * @return
     */
    public boolean foodTableStyleRunModelOpen(Long shopId) {
        List<RunModel> runModelList = findRunModelList(shopId);

        List<RunModel> modelList = runModelList.stream().filter(p -> p.getModelType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE_WAITER_QR
                || p.getModelType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE_FIX
                || p.getModelType() == Constant.RUN_MODEL_TYPE_FOOD_TABLE
        ).collect(Collectors.toList());

        //只要有一个开启了，那么就是餐桌模式开启了
        return modelList.stream().filter(p -> getBoolean(p.getEnable())).findAny().isPresent();
    }

    /**
     * 快餐模式的运营模式是否开启了
     *
     * @param shopId
     * @return
     */
    public boolean quickFoodStyleRunModelOpen(Long shopId) {
        List<RunModel> runModelList = findRunModelList(shopId);
        Optional<RunModel> totalQrRunModelOption = runModelList.stream().filter(p -> p.getModelType() == Constant.RUN_MODEL_TYPE_TOTAL).findAny();

        if (!totalQrRunModelOption.isPresent()) {
            logger.info("can not find the total qr run model for shop {}", shopId);
            return false;
        }

        //必须要是开启了，而且堂食也开启了
        RunModel totalQrRunModel = totalQrRunModelOption.get();
        if (getBoolean(totalQrRunModel.getEnable()) && getBoolean(totalQrRunModel.getTangshi())) {
            logger.info("total qr enable and tang shi enable for shop {}", shopId);
            return true;
        }

        //再判断堂食快餐模式是否开启了
        Optional<RunModel> quickModelOption = runModelList.stream().filter(p -> p.getModelType() == Constant.RUN_MODEL_TYPE_QUICK_MODEL).findAny();
        if (!quickModelOption.isPresent()) {
            logger.info("can not find the quick model for shop {}", shopId);
            return false;
        }

        RunModel quickModel = quickModelOption.get();
        return getBoolean(quickModel.getEnable());
    }
}
